from django.db import models
from django.db.models import get_model
from django.contrib.auth.models import User
from django.contrib import admin
# Create your models here.
class address(models.Model):
    user = models.ForeignKey(User)
    name = models.CharField(max_length=64)
    address = models.CharField(max_length=256)
    postnumber = models.CharField(max_length=10)
    city = models.CharField(max_length=64)
    state = models.CharField(max_length=64, blank=True)
    country = models.CharField(max_length=64)
    phone = models.CharField(max_length=16)
class order_status(models.Model):
    type = models.CharField(max_length=16)
    class Meta:
        ordering = ['id']
class order_statusAdmin(admin.ModelAdmin):
    list_display = ('type',)
    
class orders(models.Model):
    iduser = models.ForeignKey(User, related_name="orders_user")
    status = models.ForeignKey(order_status)
    created = models.DateTimeField(auto_now=True,auto_now_add=True)
    changed = models.DateTimeField(auto_now=True,auto_now_add=True)
    idaddress = models.ForeignKey(address, related_name="orders_address")
    class Meta:
        ordering = ['-changed']
    
class ordersAdmin(admin.ModelAdmin):
    list_display = ('iduser',)
    
class order_details(models.Model):
    idorder = models.ForeignKey(orders, related_name="orderDetails_orders")
    idproduct = models.ForeignKey(get_model('products','products'), related_name="orderDetails_products")
    count = models.IntegerField()
    unit_price = models.FloatField()
    
    def total(self):
        return self.unit_price*self.count
    
class product_rates(models.Model):
    order_detail = models.OneToOneField(order_details,primary_key=True)
    #product = models.ForeignKey(get_model('products','products'), related_name="productRate_products")
    #order = models.ForeignKey(orders, related_name="productRate_order")
    #user = models.ForeignKey(User, related_name="productRate_user")
    value = models.IntegerField()


admin.site.register(orders, ordersAdmin)
admin.site.register(order_status, order_statusAdmin)